package org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.recordDetailWindow;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;

import org.HisCanvas.ChrisRiffle.JFlightLog.dataModel.LogBookRec;
import org.HisCanvas.ChrisRiffle.JFlightLog.logging.LogController;





public class LogBookRecDialogController {
	
	
	private static LogBookRecDialogController INSTANCE;
	
	private static final String EDIT_DIALOG_TITLE = "Edit";
	
	private static final String ADD_DIALOG_TITLE = "Add";
	
	
	
	/**
	 * enum describes if the logbookrec dialog that is being controlled is 
	 * currently in add mode or edit mode
	 */
	public static enum WINDOW_MODE{
		ADD,
		EDIT
	}
	
	public boolean okButtonWasClicked = false;
	
	/**
	 * setOkButtonClicked
	 * @param pOkButtonWasClicked
	 */
	public void setOkButtonClicked(boolean pOkButtonWasClicked){
		
		okButtonWasClicked = pOkButtonWasClicked;
	}
	
	
	/**
	 * enum describes if the logbookrec dialog that is being controlled is 
	 * currently in add mode or edit mode
	 */	
	private WINDOW_MODE currentWindowMode = null;
	
	public WINDOW_MODE getCurrentWindowMode(){
		return currentWindowMode;
	}
	
	
	private LogBookRecDialog logBookRecDialog;
	
	
	
	private RecDetailOkCancelButtonPanelListener okCancelButtonPanelListener;
	
	
	

	
	private LogBookRecDialogController() {		
	}
	
	
	
	public static LogBookRecDialogController instance(){
		
		if(INSTANCE == null){
			
			INSTANCE = new LogBookRecDialogController();
		}
		return INSTANCE;
	}
	
	
	
	
	
	public LogBookRecDialog getLogBookRecDialog(){;
	
		if(logBookRecDialog == null){
			
			logBookRecDialog = new LogBookRecDialog();
			configDialog(logBookRecDialog);
		}
		return logBookRecDialog;
	}
	
	
	
	private RecDetailOkCancelButtonPanelListener getOkCancelButtonPanelListener() {
		
		if (okCancelButtonPanelListener == null) {
			
			okCancelButtonPanelListener = new RecDetailOkCancelButtonPanelListener(this);
		}
		return okCancelButtonPanelListener;
	}
	
	
	
	private void configDialog(LogBookRecDialog pDialog) {
		
		//reset ok button flag
		okButtonWasClicked = false;
		
		configOkButton(pDialog.getOkCancelButtonPanel().getOkButton());
		
		configCancelButton(pDialog.getOkCancelButtonPanel().getCancelButton());
		
		pDialog.addWindowListener(new WindowAdapter() {			
			@Override
			public void windowClosing(WindowEvent e) {
				//ensure we don't cache old data for the next time 
				// it gets displayed (it'll create a new one)
				logBookRecDialog = null;
			}
		});
		
		
		
	}
	
	private void configOkButton(JButton pOkButton){
		
		pOkButton.setActionCommand(RecDetailOkCancelButtonPanelListener.OK_BUTTON_CLICK);
		
		pOkButton.addActionListener(getOkCancelButtonPanelListener());
		
	}
	
	
	private void configCancelButton(JButton pCancelButton){
		
		pCancelButton.setActionCommand(RecDetailOkCancelButtonPanelListener.CANCEL_BUTTON_CLICK);
		
		pCancelButton.addActionListener(getOkCancelButtonPanelListener());
	}


	private LogBookRec currentRecForWindow;
	
	public LogBookRec getCurrentRecForWindow(){
		return currentRecForWindow;
	}
	
	/**
	 * Displays the rec detail window dialog in edit mode
	 * 
	 * @param pLogBookRec
	 * 			The rec of data to populate the form fields with
	 */
	public void displayEditFlightDialog(LogBookRec pLogBookRec){
		
		currentRecForWindow = pLogBookRec;
		
		currentWindowMode = WINDOW_MODE.EDIT;
		
		JDialog d = getLogBookRecDialog(); 
		d.setTitle(EDIT_DIALOG_TITLE);
		
		populateDetailWindowWithData(pLogBookRec);
		
		d.setLocationRelativeTo(null);
		
		d.setVisible(true);
	}
	
	
	
	public void displayAddFlightDialog(){
		
		currentRecForWindow = new LogBookRec();
		
		currentWindowMode = WINDOW_MODE.ADD;
		
		//reset dialog to null so we won't reuse trash that could still be lingering 
		// from a previous 'edit dialog' that may have been show with its fields all 
		// set with values
		logBookRecDialog = null;
		JDialog d = getLogBookRecDialog(); 
		d.setTitle(ADD_DIALOG_TITLE);
				
		d.setLocationRelativeTo(null);
		d.setVisible(true);
	}
	
	
	
	private static BigDecimal parseFloat(String pFloatValueToParse){
		
		BigDecimal rBigDecimal = null;
		
		if(pFloatValueToParse != null && !"".equals(pFloatValueToParse)){
						
			try {
				rBigDecimal = new BigDecimal(Float.parseFloat(pFloatValueToParse));
				
			} catch (NumberFormatException e) {
				
				LogController.warning("Can't parse: " + pFloatValueToParse);				
			}
		}else{
			
			rBigDecimal = new BigDecimal(0);
		}
		
		return rBigDecimal;
		
	}
	
	
	
	
	private static Short parseShort(String pShortValueToParse){
		
		Short rShort = null;
		
		if(pShortValueToParse != null && !"".equals(pShortValueToParse)){
			
			try {
				rShort = Short.parseShort(pShortValueToParse);
				
			} catch (NumberFormatException e) {

				LogController.warning("Can't parse: " + pShortValueToParse);				
			}
			
		}else{
			
			rShort = 0;
		}
		
		return rShort;		
	}
	
	
	
	
	/**
	 * updateCurrentFlightRecWithDataFromWindow
	 * @return
	 * 		the updated rec entity
	 */
	public LogBookRec updateCurrentFlightRecWithDataFromWindow() {
						
		
		if(currentRecForWindow == null){
			currentRecForWindow = new LogBookRec();
		}
		
		LogBookRecPanel recPanel = getLogBookRecDialog().getRecPanel();
		
		currentRecForWindow.setFlightDate(new Date());
							
		String dateTxt = recPanel.getDateOfFlightTxtField().getText();
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy"); 	
		
		if(dateTxt != null && !"".equals(dateTxt)){
			
			try {
				currentRecForWindow.setFlightDate(dateFormat.parse(dateTxt));
				
			} catch (ParseException e) {
				
				LogController.warning("Skipped retrieving badly formatted date");
			}
			
		}else{
			
			LogController.warning("Skipped retrieving empty date");
		}
				
		
		currentRecForWindow.setAircraftType(recPanel.getAircraftTypeTxtField().getText());
		
		currentRecForWindow.setAircraftRegistration(recPanel.getAircraftRegistrationTxtField().getText());
		
		currentRecForWindow.setDepartAirportCode(recPanel.getDepartureAirportCodeTxtField().getText());
		
		currentRecForWindow.setArrivalAirportCode(recPanel.getArrivalAirportcodeTxtField().getText());			
		
		currentRecForWindow.setLandingsTotalCount(parseShort(recPanel.getLandingsTotalCountTxtField().getText()));			
		
		currentRecForWindow.setLandingsDayCount(parseShort(recPanel.getLandingsDayCountTxtField().getText()));
		
		currentRecForWindow.setLandingsNightCount(parseShort(recPanel.getLandingsNightCountTxtField().getText()));
				
		currentRecForWindow.setSingleEngineHours(parseFloat(recPanel.getSingleEngineTxtField().getText()));
		
		currentRecForWindow.setMultiEngineHours(parseFloat(recPanel.getMultiEngineHoursTxtField().getText()));
		
		currentRecForWindow.setSoloHours(parseFloat(recPanel.getSoloTxtField().getText()));
		
		currentRecForWindow.setDayHours(parseFloat(recPanel.getDayHoursTxtField().getText()));
		
		currentRecForWindow.setNightHours(parseFloat(recPanel.getNightHoursTxtField().getText()));
		
		currentRecForWindow.setInstrumentActualHours(parseFloat(recPanel.getActualInstrumentTxtField().getText()));
		
		currentRecForWindow.setInstrumentSimHours(parseFloat(recPanel.getSimInstrumentHoursTxtField().getText()));
						
		currentRecForWindow.setIapCount(parseShort(recPanel.getIapCountTxtField().getText()));
							
		currentRecForWindow.setIapNames(recPanel.getIapNamesTxtField().getText());
		
		currentRecForWindow.setSafetyPilotName(recPanel.getSafetyPilotTxtField().getText());
		
		currentRecForWindow.setSimHours(parseFloat(recPanel.getSimulatorTxtField().getText()));
		
		currentRecForWindow.setDualReceivedHours(parseFloat(recPanel.getDualReceivedTxtField().getText()));
		
		currentRecForWindow.setDualGivenHours(parseFloat(recPanel.getDualGivenTxtField().getText()));
		
		currentRecForWindow.setPicHours(parseFloat(recPanel.getPicTxtField().getText()));
		
		currentRecForWindow.setSicHours(parseFloat(recPanel.getSicHoursTxtField().getText()));
		
		currentRecForWindow.setxCountryHours(parseFloat(recPanel.getxCountryHoursTxtField().getText()));
		
		currentRecForWindow.setTotalHours(parseFloat(recPanel.getTotalHoursTxtField().getText()));
		
		currentRecForWindow.setRemarks(recPanel.getRemarksTxtField().getText());
		
		return currentRecForWindow;		
		
	}
	

	/**
	 * Populates the log book rec detail window with data from corresponding fields
	 * in the log book rec object
	 * 
	 * @param pLogBookRec
	 * 			 The rec to pull data out of and put into the window's form fields
	 */
	public void populateDetailWindowWithData(LogBookRec pLogBookRec) {
		
							
		LogBookRecPanel recPanel = getLogBookRecDialog().getRecPanel();
			
		Date flightDate = pLogBookRec.getFlightDate();		
		
		String dateForWindow = null;
		if(flightDate != null){
			SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");				
			dateForWindow = sdf.format(flightDate);			
		}
 		
		
		recPanel.getDateOfFlightTxtField().setText(dateForWindow);
				
		recPanel.getAircraftTypeTxtField().setValue(pLogBookRec.getAircraftType());
		recPanel.getAircraftRegistrationTxtField().setValue(pLogBookRec.getAircraftRegistration());
		recPanel.getDepartureAirportCodeTxtField().setValue(pLogBookRec.getDepartAirportCode());
		recPanel.getArrivalAirportcodeTxtField().setValue(pLogBookRec.getArrivalAirportCode());
		recPanel.getLandingsTotalCountTxtField().setValue(pLogBookRec.getLandingsTotalCount());
		recPanel.getLandingsDayCountTxtField().setValue(pLogBookRec.getLandingsDayCount());
		recPanel.getLandingsNightCountTxtField().setValue(pLogBookRec.getLandingsNightCount());
		recPanel.getSingleEngineTxtField().setValue(pLogBookRec.getSingleEngineHours());
		recPanel.getMultiEngineHoursTxtField().setValue(pLogBookRec.getMultiEngineHours());
		recPanel.getSoloTxtField().setValue(pLogBookRec.getSoloHours());
		recPanel.getDayHoursTxtField().setValue(pLogBookRec.getDayHours());
		recPanel.getNightHoursTxtField().setValue(pLogBookRec.getNightHours());
		recPanel.getActualInstrumentTxtField().setValue(pLogBookRec.getInstrumentActualHours());
		recPanel.getSimInstrumentHoursTxtField().setValue(pLogBookRec.getInstrumentSimHours());
		recPanel.getIapCountTxtField().setValue(pLogBookRec.getIapCount());
		recPanel.getIapNamesTxtField().setValue(pLogBookRec.getIapNames());
		recPanel.getSafetyPilotTxtField().setValue(pLogBookRec.getSafetyPilotName());
		recPanel.getSimulatorTxtField().setValue(pLogBookRec.getSimHours());
		recPanel.getDualReceivedTxtField().setValue(pLogBookRec.getDualReceivedHours());
		recPanel.getDualGivenTxtField().setValue(pLogBookRec.getDualGivenHours());
		recPanel.getPicTxtField().setValue(pLogBookRec.getPicHours());
		recPanel.getSicHoursTxtField().setValue(pLogBookRec.getSicHours());
		recPanel.getxCountryHoursTxtField().setValue(pLogBookRec.getSicHours());
		recPanel.getTotalHoursTxtField().setValue(pLogBookRec.getTotalHours());
		recPanel.getRemarksTxtField().setValue(pLogBookRec.getRemarks());
						
	}

	
	
}

